package com.kinon.gfcanteen_v1.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kinon.gfcanteen_v1.MainActivity;
import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.network.CommonSubscriber;
import com.kinon.gfcanteen_v1.network.HttpManage;
import com.kinon.gfcanteen_v1.network.StrCallback;
import com.kinon.gfcanteen_v1.util.RxUtil;
import com.kinon.gfcanteen_v1.util.SPManager;
import com.kinon.gfcanteen_v1.view.base.BaseActivity;

import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2018-01-09.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private TextView tv_title;
    private EditText et_phone;
    private EditText et_password;
    private Button btn_login;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_layout);
        initView();
    }
    private void initView() {
        tv_title= (TextView) findViewById(R.id.tv_title);
        et_phone= (EditText) findViewById(R.id.et_phone);
        et_password= (EditText) findViewById(R.id.et_password);
        btn_login= (Button) findViewById(R.id.btn_login);
        tv_title.setText("登录");
        et_phone.setText(SPManager.getInstance().getStr("phone"));
        et_password.setText(SPManager.getInstance().getStr("password"));
        addListener();
    }

    private void addListener(){
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        isPhone();
    }

    private void isPhone(){
        String phone=et_phone.getText().toString();
        if(TextUtils.isEmpty(phone)){
            mToast("请输入账号");
            return;
        }
        String password=et_password.getText().toString();
        if(TextUtils.isEmpty(password)){
            mToast("请输入密码");
            return;
        }
        login(phone,password);
    }

    private void login(final String phone,final String password){
        Call<String> call=HttpManage.getInstance().login(phone,password);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                SPManager.getInstance().putStr("token",data);
                SPManager.getInstance().putStr("phone",phone);
                SPManager.getInstance().putStr("password",password);
                SPManager.getInstance().commit();
                Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onError(int code, String error) {
              mToast(error);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
